package com.ruoyi.api.web;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shop.domain.ShopUser;
import com.ruoyi.shop.domain.ShopUserOpenid;
import com.ruoyi.shop.service.IShopUserOpenidService;
import com.ruoyi.shop.service.IShopUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "API登录授权接口")
@RestController
@RequestMapping("/api/auth")
public class ApiAuthController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IShopUserService shopUserService;
    @Autowired
    private IShopUserOpenidService shopUserOpenidService;


    /**
     * 登录
     */
    @ApiOperation(value = "登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
    })
    @GetMapping("/login")
    public AjaxResult login(String mobile, String password) throws Exception {
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            return AjaxResult.error("手机号/密码不能为空");
        }
        //用户登录
        ShopUser shopUser = shopUserService.login(mobile, password);

        ShopUserOpenid shopUserOpenid = shopUserOpenidService.selectShopUserOpenidById(shopUser.getId());

        if(StringUtils.isEmpty(shopUserOpenid.getSessionKey())){
            //生成token
            //tokenService.createToken(userId);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("token", shopUserOpenid.getSessionKey());
        map.put("userInfo", shopUser);
        return AjaxResult.success(map);
    }

}
